﻿using System;
using FileUtility.FTP.FtpSupport;

namespace TXTMonitor
{
    internal class DespatchFTPProcess:ProcessFileBase,IDisposable
    {
        #region Private Members
        private FtpConnection ftpConnection;
        private string _ftpFolderPath;
        private bool isInitialize;
        #endregion

        #region Public Properties
        public string FtpFolderPath
        {
            get
            {
                return this._ftpFolderPath;
            }
            set
            {
                this._ftpFolderPath = value;
            }
        }
        #endregion

        #region Constuctor
        public DespatchFTPProcess()
        {
            
        }
        #endregion

        #region ProcessFileBase Members

        public override void Initialize(ProcessType processType, System.Collections.Specialized.NameValueCollection config)
        {
            if ((!string.IsNullOrEmpty(config["FTPPath"])))
            {//&& (!string.IsNullOrEmpty(config["FTPUserName"])) && (!string.IsNullOrEmpty(config["FTPPassword"]))
                string ftpPath = config["FTPPath"];
                string ftpUserName = config["FTPUserName"];
                string ftpPassword = config["FTPPassword"];
                int iPort = 21;
                if (!string.IsNullOrEmpty(config["IPort"]))
                {
                    Int32.TryParse(config["IPort"], out iPort);
                }
                if (!string.IsNullOrEmpty(config["FTPFolderPath"]))
                {
                   this._ftpFolderPath= config["FTPFolderPath"];
                }

                ftpConnection = new FtpConnection(ftpPath, ftpUserName, ftpPassword, iPort);
                ftpConnection.SetCurrentDirectory(this._ftpFolderPath);
                this.isInitialize = true;
            }
            else
            {
                throw new Exception("FTP Conntection Error");
            }
            base.Initialize(processType, config);
        }

        public override void ProcessFile(string PlantCode, string FileName, string filePath)
        {
            if (this.isInitialize)
            {
                this.ftpConnection.PutFile(filePath, FileName);
            }
            else
            {
                throw new Exception("Must Initialize");
            }
        }
         #endregion

        #region IDisposable Member
        public void Dispose()
        {
            this.ftpConnection = null;
        }
        #endregion
    }
}
